package com.eras.shoes.contents;

import android.util.Log;

import com.eras.shoes.utils.ByteUtils;

import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 蓝牙数据解析
 * 1、帧头不转换字节序
 * 2、校验和不需要转换字节序
 * 其他需要转换字节序；数据中根据文档包含实时数据和硬件数据，但是实际数据中不包含硬件数据
 * liujun 2024-07-27
 */
public class BleDataEx {

    private final static short DATA_EXCLUDE_CONTENT_SIZE = 11;      //出去通道值数据的其他数据的大小
    private String drivceName;
    private String address;
    private short frameHead = 0x55AA;        //帧头,默认55aa
    private short dataLen = 0;               //数据长度
    private byte cmd = 0;                    //控制命令， 设备数据：0x44  (Data) 主机命令：0x43  (Cmd) 从机应答：0x41  (Ack) 固件更新：0x42  (Bin)

    private int frameTotal = 0;             //帧总数
    private int frame = 0;                  //帧数，第几帧，从0开始

    public class BleData{
        public long timestamp;              //时间戳
        public long mRecvTimestamp;              //接收时间戳
        public long uniqueTimestamp;        //唯一时间戳
        public List<Float> dataList = new ArrayList<>();    //数据内容
        public List<Float> dataListTest = new ArrayList<>();    //测试专用

        public String firmwareVersion;        //固件版本号 10
        public String hardwareVersion;         //硬件版本号 6
        public String macAddress;              //Mac地址 6
        public byte transmissionPower;         //蓝牙发射功率
        public float realTimeCaptureFrequency; // 实时数据采集频率	4	浮点数，应该是整数
        public float realTimeUploadCaptureFrequency; //实时数据上报频率	4	浮点数，应该是整数
        public float conventionCaptureFrequency;   //常规数据采集频率	4	浮点数，应该是整数
        public float conventionUploadCaptureFrequency; //常规数据上报频率	4	浮点数，应该是整数
    }

    private List<BleData> dataList = new ArrayList<>();

    private short crc16Modbus = 0;       //CRC-16/MODBUS 校验
    private static boolean isNeedHostToNet = false; //需要转换网络字节序
    private static final String TAG = "BleData";
    static {
//        int n = 0x12346678;
//        isNeedHostToNet = ((n >> 24) & 0xff) != 0x12;
        isNeedHostToNet = true; //强制转换网络字节序
    }


    public BleDataEx(){

    }

    public BleDataEx(String drivceName, String address){
        this.drivceName = drivceName;
        this.address = address;
    }
    //测试专用方法
    public void parseDataTypeTest(byte[] data, String name) throws Exception {
        String shouse = "";
        if (name.contains("LEFT") || name.contains("左")) {//区分左右脚
            shouse = "左脚";
        } else {
            shouse = "右脚";
        }
        int index = 0;
        //帧头
        this.frameHead = (short)((((data[0]) & 0x00FF) << 8) | (data[1] & 0x00FF));
        index += 2;
        //验证数据
        if(this.frameHead != 0x55AA) throw new Exception("数据验证失败，帧头0x55AA验证失败！");

        this.dataLen = getShortValue(data,index);    //有效字节,当前帧从命令字到数据内容一共多少有效字节
        index += 2;

        //验证CRC16校验
        int crc16Value = crc16Modbus(data,(short)0,(short)(this.dataLen + 4)); //当前帧从命令字到数据内容一共多少有效字节 + 数据长度2 + 帧头2

        //获取数据中CRC16校验值
        int dataCrc16Value = getShortValueNoNtoH(data,this.dataLen + 4);
        String data1 = getData(data);
        String crc = getCRC(data, (short) (this.dataLen + 4));
        if (!crc.equals(data1.substring(data1.length()-4).toUpperCase())){
            throw new Exception("数据验证失败，CRC16校验值验证失败！");
        }
//        if(crc16Value != dataCrc16Value){
//            Log.i(TAG, "失败 校验值:" + crc16Value + " 数据中CRC16: "+dataCrc16Value+" crc: "+crc+"  data: "+ data1.substring(data1.length()-4).toUpperCase());
//            throw new Exception("数据验证失败，CRC16校验值验证失败！");
//        }else {
//            Log.i(TAG, "成功 校验值:" + crc16Value + " 数据中CRC16: "+dataCrc16Value+" crc: "+crc+"  data: "+ data1.substring(data1.length()-4).toUpperCase());
//        }
        this.cmd = data[index];     //命令字
        index++;
        this.frameTotal = getThreeByteValue(data,index);             //帧总数
        index+=3;
        this.frame = getThreeByteValue(data,index);                //帧数
        index+=3;

        //验证数据大小
        int totalSize = DATA_LEN * this.frameTotal + 7; //(时间戳6 + 唯一标识时间戳6 + 实时数据104 + 硬件数据39) * 帧数 + 1字节命令字 + 3字节帧总数 + 3字节第几帧
        if(totalSize != this.dataLen) throw new Exception("数据验证失败，计算的数据大小和数据包中数据大小不一致！");

        //读取数据
//        for(int i = 0;i<this.frameTotal;i++){
            BleData bleData = new BleData();
            bleData.mRecvTimestamp=System.currentTimeMillis();
            //读取时间戳
            bleData.timestamp = getSixByteValue(data,index);
//        Log.i(TAG, "writeData 时间戳" + getSixByteValue(data,index));
//            Log.i(TAG, "writeData 时间戳" + new Date(getSixByteValue(data,index)));
//            String timestamp = String.valueOf(new Date(getSixByteValue(data,index)));


                index += 6;
            //读取唯一标识时间戳
            bleData.uniqueTimestamp = getSixByteValue(data,index);
//        Log.i(TAG, "writeData 唯一标识时间戳" + getSixByteValue(data,index));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date date = new Date(getSixByteValue(data,index));
        String timestamp = format.format(date);
//        Log.i(TAG, "writeData 唯一标识时间戳" + timestamp);
            index += 6;

            bleData.dataListTest.add(getFloatValue(data,index));        // 压力传感器1	4	浮点数
            float one = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //压力传感器2	4	浮点数
            float two = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //压力传感器3	4	浮点数
            float three = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //压力传感器4	4	浮点数
            float four = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //压力传感器5	4	浮点数
            float five = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //压力传感器6	4	浮点数
            float six = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //压力传感器7	4	浮点数
            float seven = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //压力传感器8	4	浮点数
            float eight = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //压力传感器9	4	浮点数
            float nine = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //压力传感器10	4	浮点数
            float ten = getFloatValue(data,index);
            String addPress = addPress(one,two,three,four,five,six,seven,eight,nine,ten);


            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //加速度x轴	4	浮点数
            float ax = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //加速度y轴	4	浮点数
            float ay = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //加速度z轴	4	浮点数
            float az = getFloatValue(data,index);
            String addSpeed = addSpeed(ax, ay, az);

            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //陀螺仪x轴	4	浮点数
            float gx = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //陀螺仪y轴	4	浮点数
            float gy = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //陀螺仪z轴	4	浮点数
            float gz = getFloatValue(data,index);
            String addGyro = addGyro(gx, gy, gz);

            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //地磁计x轴	4	浮点数
            float ex = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //地磁计y轴	4	浮点数
            float ey = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //地磁计z轴	4	浮点数
            float ez = getFloatValue(data,index);
            String addEarth = addEarth(ex, ey, ez);

            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //姿态角z(yaw)	4	浮点数
            float anz = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //姿态角y(pitch)	4	浮点数
            float any = getFloatValue(data,index);
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //姿态角x(roll)	4	浮点数
            float anx = getFloatValue(data,index);
            String addAngle = addAngle(anx, any, anz);

            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //电池剩余电量	4	浮点数
            index += 4;
            bleData.dataListTest.add((float)data[index]);               // 蓝牙状态	1	"0x00 蓝牙关闭 0x01 蓝牙开放但未连接 0x02 蓝牙开放并已连接"
            index++;
            bleData.dataListTest.add((float)data[index]);               //蓝牙信号强度	1	0 ~ -255 dBm
            index++;
            bleData.dataListTest.add((float)data[index]);               //Wifi状态	1	"0x00 Wifi关闭 0x01 Wifi开放但未连接 0x02 Wifi开放并已连接"
            index++;
            bleData.dataListTest.add((float)data[index]);               //Wifi信号强度	1	0 ~ -255 dBm
            index++;
            bleData.dataListTest.add(getFloatValue(data,index));        //温度	4	浮点数-20.0~+60.0C
//            Log.i(TAG, "writeData =温度=" + getFloatValue(data,index));
            String temp = String.valueOf(getFloatValue(data,index));
            index += 4;
            bleData.dataListTest.add(getFloatValue(data,index));        //压力	4	浮点数。如果不够长，可增加。
//            Log.i(TAG, "writeData =压力=" + getFloatValue(data,index));
            String press = String.valueOf(getFloatValue(data,index));
//        Log.i(TAG, "writeData"+"时间戳"+"\t"+"压力传感器");
            Log.i("writeData", "\t"+shouse+"\t"+data1+"\t"+"time"+timestamp+"\t"+addPress+"\t"+addSpeed+"\t"+addGyro+"\t"+addEarth+"\t"+addAngle+"\t"+temp+"\t"+press);
            index += 4;

//            bleData.firmwareVersion = getStringValue(data,index,10);    //固件版本号 10
//            index += 10;
//            bleData.hardwareVersion = getStringValue(data,index,6);    //硬件版本号 6
//            index += 6;
//            bleData.macAddress = getStringValue(data,index,6);         //Mac地址 6
//            index += 6;
//            bleData.transmissionPower = data[index];        //蓝牙发射功率
//            index++;
//            bleData.realTimeCaptureFrequency = getFloatValue(data,index);               // 实时数据采集频率	4	浮点数，应该是整数
//            index += 4;
//            bleData.realTimeUploadCaptureFrequency = getFloatValue(data,index);         //实时数据上报频率	4	浮点数，应该是整数
//            index += 4;
//            bleData.conventionCaptureFrequency = getFloatValue(data,index);             //常规数据采集频率	4	浮点数，应该是整数
//            index += 4;
//            bleData.conventionUploadCaptureFrequency = getFloatValue(data,index);       //常规数据上报频率	4	浮点数，应该是整数
//            index += 4;
//            dataLisTest.add(bleData);
//        }
    }

    private final static short DATA_LEN = 116;      //数据内容的长度，时间戳6 + 唯一标识时间戳6 + 实时数据104 //+ 硬件数据39 不加硬件数据
    /**
     * 解析数据
     * @param data
     */
    public void parseData(byte[] data) throws Exception {
        int index = 0;
        //帧头
        this.frameHead = (short)((((data[0]) & 0x00FF) << 8) | (data[1] & 0x00FF));
        index += 2;
        //验证数据
        if(this.frameHead != 0x55AA) throw new Exception("数据验证失败，帧头0x55AA验证失败！");

        this.dataLen = getShortValue(data,index);    //有效字节,当前帧从命令字到数据内容一共多少有效字节
        index += 2;

        //验证CRC16校验
        int crc16Value = crc16Modbus(data,(short)0,(short)(this.dataLen + 4)); //当前帧从命令字到数据内容一共多少有效字节 + 数据长度2 + 帧头2

        //获取数据中CRC16校验值
        int dataCrc16Value = getShortValueNoNtoH(data,this.dataLen + 4);
        String data1 = getData(data);
        String crc = getCRC(data, (short) (this.dataLen + 4));
        if (!crc.equals(data1.substring(data1.length()-4).toUpperCase())){
            throw new Exception("数据验证失败，CRC16校验值验证失败！");
        }
//        if(crc16Value != dataCrc16Value){
//            Log.i(TAG, "失败 校验值:" + crc16Value + " 数据中CRC16: "+dataCrc16Value+" crc: "+crc+"  data: "+ data1.substring(data1.length()-4).toUpperCase());
//            throw new Exception("数据验证失败，CRC16校验值验证失败！");
//        }else {
//            Log.i(TAG, "成功 校验值:" + crc16Value + " 数据中CRC16: "+dataCrc16Value+" crc: "+crc+"  data: "+ data1.substring(data1.length()-4).toUpperCase());
//        }
        this.cmd = data[index];     //命令字
        index++;
        this.frameTotal = getThreeByteValue(data,index);             //帧总数
        index+=3;
        this.frame = getThreeByteValue(data,index);                //帧数
        index+=3;

        //验证数据大小
        int totalSize = DATA_LEN * this.frameTotal + 7; //(时间戳6 + 唯一标识时间戳6 + 实时数据104 + 硬件数据39) * 帧数 + 1字节命令字 + 3字节帧总数 + 3字节第几帧
        if(totalSize != this.dataLen) throw new Exception("数据验证失败，计算的数据大小和数据包中数据大小不一致！");

        //读取数据
        for(int i = 0;i<this.frameTotal;i++){
            BleData bleData = new BleData();
            bleData.mRecvTimestamp=System.currentTimeMillis();
            //读取时间戳
            bleData.timestamp = getSixByteValue(data,index);
            index += 6;
            //读取唯一标识时间戳
            bleData.uniqueTimestamp = getSixByteValue(data,index);
            index += 6;

            bleData.dataList.add(getFloatValue(data,index));        // 压力传感器1	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //压力传感器2	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //压力传感器3	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //压力传感器4	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //压力传感器5	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //压力传感器6	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //压力传感器7	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //压力传感器8	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //压力传感器9	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //压力传感器10	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //加速度x轴	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //加速度y轴	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //加速度z轴	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //陀螺仪x轴	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //陀螺仪y轴	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //陀螺仪z轴	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //地磁计x轴	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //地磁计y轴	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //地磁计z轴	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //姿态角z(yaw)	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //姿态角y(pitch)	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //姿态角x(roll)	4	浮点数
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //电池剩余电量	4	浮点数
            index += 4;
            bleData.dataList.add((float)data[index]);               // 蓝牙状态	1	"0x00 蓝牙关闭 0x01 蓝牙开放但未连接 0x02 蓝牙开放并已连接"
            index++;
            bleData.dataList.add((float)data[index]);               //蓝牙信号强度	1	0 ~ -255 dBm
            index++;
            bleData.dataList.add((float)data[index]);               //Wifi状态	1	"0x00 Wifi关闭 0x01 Wifi开放但未连接 0x02 Wifi开放并已连接"
            index++;
            bleData.dataList.add((float)data[index]);               //Wifi信号强度	1	0 ~ -255 dBm
            index++;
            bleData.dataList.add(getFloatValue(data,index));        //温度	4	浮点数-20.0~+60.0C
            index += 4;
            bleData.dataList.add(getFloatValue(data,index));        //压力	4	浮点数。如果不够长，可增加。
            index += 4;

//            bleData.firmwareVersion = getStringValue(data,index,10);    //固件版本号 10
//            index += 10;
//            bleData.hardwareVersion = getStringValue(data,index,6);    //硬件版本号 6
//            index += 6;
//            bleData.macAddress = getStringValue(data,index,6);         //Mac地址 6
//            index += 6;
//            bleData.transmissionPower = data[index];        //蓝牙发射功率
//            index++;
//            bleData.realTimeCaptureFrequency = getFloatValue(data,index);               // 实时数据采集频率	4	浮点数，应该是整数
//            index += 4;
//            bleData.realTimeUploadCaptureFrequency = getFloatValue(data,index);         //实时数据上报频率	4	浮点数，应该是整数
//            index += 4;
//            bleData.conventionCaptureFrequency = getFloatValue(data,index);             //常规数据采集频率	4	浮点数，应该是整数
//            index += 4;
//            bleData.conventionUploadCaptureFrequency = getFloatValue(data,index);       //常规数据上报频率	4	浮点数，应该是整数
//            index += 4;
            dataList.add(bleData);
        }
    }

    private String addAngle(float x,float y,float z){
        Float[] angle = new Float[3];
        angle[0] = x;
        angle[1] = y;
        angle[2] = z;
//        Log.i(TAG, "writeData =姿态角=" + Arrays.toString(angle));
        return Arrays.toString(angle);
    }

    private String addEarth(float x,float y,float z){
        Float[] earth = new Float[3];
        earth[0] = x;
        earth[1] = y;
        earth[2] = z;
//        Log.i(TAG, "writeData =地磁计=" + Arrays.toString(earth));
        return Arrays.toString(earth);
    }

    private String addGyro(float x,float y,float z){
        Float[] gyro = new Float[3];
        gyro[0] = x;
        gyro[1] = y;
        gyro[2] = z;
//        Log.i(TAG, "writeData =陀螺仪=" + Arrays.toString(gyro));
        return Arrays.toString(gyro);
    }

    private String addSpeed(float x,float y,float z){
        Float[] speed = new Float[3];
        speed[0] = x;
        speed[1] = y;
        speed[2] = z;
//        Log.i(TAG, "writeData =加速度=" + Arrays.toString(speed));
        return Arrays.toString(speed);
    }

    private String addPress(float one,float two,float three,float four,float five,float six,float seven,float eight,float nine,float ten){
        Float[] press = new Float[10];
        press[0] = one;
        press[1] = two;
        press[2] = three;
        press[3] = four;
        press[4] = five;
        press[5] = six;
        press[6] = seven;
        press[7] = eight;
        press[8] = nine;
        press[9] = ten;
//        Log.i(TAG, "writeData =压力传感器=" + Arrays.toString(press));
        return Arrays.toString(press);
    }

    /**
     * 获取字符串内容
     * @param data
     * @param index
     * @param len
     * @return
     */
    public static String getStringValue(byte[] data, int index, int len){
        byte[] strBuffer = new byte[len];
        for(int i = 0;i<len;i++){
            strBuffer[i] = data[index + i];
        }
        Log.i(TAG,"设备ScanRecordbytes[]="+ ByteUtils.INSTANCE.bytesToHexString(strBuffer));
        return new String(strBuffer);
    }

    private String getData(byte[] dataBuffer){
        StringBuilder stringBuilder =new StringBuilder();
        for (int i = 0; i < dataBuffer.length; i++) {
            String hexString = Integer.toHexString(dataBuffer[i] & 0xFF);
            if (hexString.length() == 1){
                stringBuilder.append("0");
            }
            stringBuilder.append(hexString);
        }
        return stringBuilder.toString();
    }

    /**
     * 计算CRC16/Modbus校验码  低位在前,高位在后
     *
     * @param bytes 十六进制字符串
     * @return
     */
    public static String getCRC(byte[] bytes,short index) {
        int CRC = 0x0000ffff;
        int POLYNOMIAL = 0x0000a001;

        int i, j;
        for (i = 0; i < index; i++) {
            CRC ^= ((int) bytes[i] & 0x000000ff);
            for (j = 0; j < 8; j++) {
                if ((CRC & 0x00000001) != 0) {
                    CRC >>= 1;
                    CRC ^= POLYNOMIAL;
                } else {
                    CRC >>= 1;
                }
            }
        }
        String crc = Integer.toHexString(CRC);
        if (crc.length() == 2) {
            crc = "00" + crc;
        } else if (crc.length() == 3) {
            crc = "0" + crc;
        }
        crc = crc.substring(0, 2) + crc.substring(2, 4) ;
        return crc.toUpperCase();
    }
    /**
     * 获取2个字节的数据
     * @param data
     * @param index
     * @return
     */
    public short getShortValue(byte[] data,int index){
        short value = 0;
        if(isNeedHostToNet){
            value = data[index + 1];
            value = (short) (value << 8 & 0xFF00);
            value |= (0x00ff & data[index]);
        }else {
            value = data[index];
            value = (short) (value << 8 & 0xFF00);
            value |= (0x00ff & data[index + 1]);
        }
        return value;
    }

    /**
     * 不转换网络字节序
     * @param data
     * @param index
     * @return
     */
    public short getShortValueNoNtoH(byte[] data,int index){
        short value = 0;
        value = data[index];
        value = (short) (value << 8 & 0xFF00);
        value |= (0x00ff & data[index + 1]);
        return value;
    }

    /**
     * 获取三个字节的数据
     * @param data
     * @param index
     * @return
     */
    public int getThreeByteValue(byte[] data,int index){
        int value = 0;
        value = ((data[index]) & 0x000000ff) | (((data[index + 1] & 0x000000ff) << 8) & 0x0000ff00) | (((data[index + 2] & 0x000000ff) << 16) & 0x00ff0000);
        return value;
    }

    /**
     * 读取6个字节的时间戳 0000D2C7CA000000 000000CAC7D20000
     * @param data
     * @param index
     * @return
     */
    public long getSixByteValue(byte[] data,int index){
        //Log.i("getSixByteValue=",""+index);
        byte[] values = new byte[6];
        if(isNeedHostToNet){
            values[5] = data[index++];
            values[4] = data[index++];
            values[3] = data[index++];
            values[2] = data[index++];
            values[1] = data[index++];
            values[0] = data[index++];
        }else {
            values[0] = data[index++];
            values[1] = data[index++];
            values[2] = data[index++];
            values[3] = data[index++];
            values[4] = data[index++];
            values[5] = data[index++];
        }
        StringBuilder stringBuilder =new StringBuilder();
        for (byte b : values) {
            String hexString = Integer.toHexString(b & 0xFF);
            if (hexString.length() == 1){
                stringBuilder.append("0");
            }
            stringBuilder.append(hexString);
        }
        return Long.parseLong(stringBuilder.toString(),16);

//        ByteBuffer byteBuffer = ByteBuffer.wrap(values);
//        return byteBuffer.getLong();
//        return ((data[index+5] & 0x00000000000000ff) << 40) | ((data[index+4] & 0x00000000000000ff) << 32)
//                | ((data[index+3] & 0x00000000000000ff) << 24) | ((data[index+2] & 0x00000000000000ff) << 16)
//                | ((data[index+1] & 0x00000000000000ff) << 8) | (data[index] & 0x00000000000000ff);
    }

    /**
     * CRC 16 MODBUS实现
     * @param data
     * @return
     */
    private final static int POLY = 0xA001;
    public static int crc16Modbus(byte[] data,short offset,short dataLen) {
        byte b = 0;
        int crc = 0xFFFF;
        for(int j = offset;j<dataLen;j++){
            b = data[j];

            crc ^= b & 0xFF;
            for (int i = 0; i < 8; i++) {
                if ((crc & 0x0001) != 0) {
                    crc >>= 1;
                    crc ^= POLY;
                } else {
                    crc >>= 1;
                }
            }
        }
        if(isNeedHostToNet) return crc;

        //改变位置
        short v = (short)(crc >> 8 & 0x00FF);
        crc = (short)((((crc & 0x00FF) << 8) & 0xFF00) | v);
        return crc;
    }


    /**
     * 设置通道数据
     * @param buffer
     * @param b
     * @param index
     */
    public int setValue(byte[] buffer,float b,int index){
        ByteBuffer byteBuffer = ByteBuffer.allocate(4);
        byteBuffer.putFloat(b);
        byte[] floatBytes = byteBuffer.array();
        Log.d(TAG, "解析前的数据"+byteBuffer.toString() );
        if(isNeedHostToNet){
            index = setValue(buffer,floatBytes[3],index);
            index = setValue(buffer,floatBytes[2],index);
            index = setValue(buffer,floatBytes[1],index);
            return setValue(buffer,floatBytes[0],index);
        }else{
            index = setValue(buffer,floatBytes[0],index);
            index = setValue(buffer,floatBytes[1],index);
            index = setValue(buffer,floatBytes[2],index);
            return setValue(buffer,floatBytes[3],index);
        }
    }

    /**
     * 数据复制
     * @param buffer
     * @param b
     * @param index
     */
    private int setValue(byte[] buffer,short b,int index){
        if(isNeedHostToNet){
            index = setValue(buffer, (byte) (b & 0x00FF), index);
            return setValue(buffer, (byte) ((b >> 8) & 0x00FF), index);
        }else {
            index = setValue(buffer, (byte) (b >> 8 & 0x00FF), index);
            return setValue(buffer, (byte) (b & 0x00FF), index);
        }
    }


    /**
     * 设置数据值
     * @param buffer
     * @param b
     * @param index
     */
    private int setValue(byte[] buffer,byte b,int index){
        buffer[index] = b;
        return ++index;
    }


    public static String getByteToString(byte[] date){
        StringBuilder stringBuilder =new StringBuilder();
        for (int i = 0; i < date.length; i++) {
            String hexString = Integer.toHexString(date[i] & 0xFF);
            if (hexString.length() == 1){
                stringBuilder.append("0");
            }
            stringBuilder.append(hexString);
        }
        return stringBuilder.toString();
    }





    /**
     * 获取float的数据
     * @param data
     * @param index
     * @return
     */
    public float getFloatValue(byte[] data,int index){
        byte[] values = new byte[4];
        if(isNeedHostToNet){
            values[3] = data[index++];
            values[2] = data[index++];
            values[1] = data[index++];
            values[0] = data[index++];
        }else {
            values[0] = data[index++];
            values[1] = data[index++];
            values[2] = data[index++];
            values[3] = data[index++];
        }
        ByteBuffer byteBuffer = ByteBuffer.wrap(values);
        return byteBuffer.getFloat();
    }

    public String getDrivceName() {
        return drivceName;
    }

    public void setDrivceName(String drivceName) {
        this.drivceName = drivceName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public short getFrameHead() {
        return frameHead;
    }

    public void setFrameHead(short frameHead) {
        this.frameHead = frameHead;
    }

    public short getDataLen() {
        return dataLen;
    }

    public void setDataLen(short dataLen) {
        this.dataLen = dataLen;
    }

    public byte getCmd() {
        return cmd;
    }

    public void setCmd(byte cmd) {
        this.cmd = cmd;
    }

    public int getFrameTotal() {
        return frameTotal;
    }

    public void setFrameTotal(int frameTotal) {
        this.frameTotal = frameTotal;
    }

    public int getFrame() {
        return frame;
    }

    public void setFrame(int frame) {
        this.frame = frame;
    }

    public List<BleData> getDataList() {
        return dataList;
    }

    public void setDataList(List<BleData> dataList) {
        this.dataList = dataList;
    }

    public static String hexToASCII(String hexStr) {
        // 验证输入是否合法
        if (hexStr == null || hexStr.length() % 2 != 0) {
            throw new IllegalArgumentException("无效的十六进制字符串");
        }
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < hexStr.length(); i += 2) {
            // 将两个十六进制字符转换为字节
            String hexByte = hexStr.substring(i, i + 2);
            int decimal = Integer.parseInt(hexByte, 16);  // 将16进制转换为10进制
            output.append((char) decimal);  // 将10进制转换为对应的ASCII字符并添加到结果中
        }
        return output.toString();
    }
}
